Scheduled Processes Lab

1. Schedule System Cron Jobs

In this lab, you work with recurring system jobs. You run a daily job to count the number of active users, and an updated cron job to gather system performance data.

  1. Log in to your desktop1.example.com system as student, then elevate your privileges to root.

    [student@desktop1 ~]$ su -
    Password: r3dh@t1!
  2. Create a new daily cron job that logs a message to the system log with the number of currently active users (w -h | wc -l). You can use the logger command to send messages to the system log.

    1. Using an editor, open a new file in /etc/cron.daily, e.g. /etc/cron.daily/usercount.

      [root@desktop1 ~]# vim /etc/cron.daily/usercount
    2. Write the script that logs the number of active users to the system log. Insert the following in your editor:

      #!/bin/bash
        USERCOUNT=$(w -h | wc -l)
        logger "There are currently ${USERCOUNT} active users"
    3. Make the script executable.

      [root@desktop1 ~]# chmod +x /etc/cron.daily/usercount
  3. The sysstat package, when installed, has a cron job that runs every 10 minutes, collecting data using a command called sa1. Make sure this package is installed, then change this job to run every five minutes.

    1. Make sure the sysstat package is installed.

      [root@desktop1 ~]# yum -y install sysstat
    2. Find out in which file the sysstat package has configured the cron jobs. Cron jobs are generally configured in files marked as a configuration file for the package manager.

      [root@desktop1 ~]# rpm -qc sysstat
      • /etc/cron.d/sysstat looks promising.

    3. Open /etc/cron.d/sysstat in an editor.

      [root@desktop1 ~]# vim /etc/cron.d/sysstat
    4. Change */10 on the sa1 line to */5, then save your changes and exit.

    5. Monitor the files in /var/log/sa to see when their sizes and timestamps change.

      [root@desktop1 ~]# watch ls -l /var/log/sa

2. Manage Temporary Files

In this lab, you configure your system to purge files older than 5 days from /tmp. You also add a new automatically created temporary directory called /run/gallifrey, in which files that have been unused for more than 30 seconds are automatically purged.

  1. Reset your server1.example.com system.

  2. /tmp is under systemd-tmpfiles control. To override the upstream settings, copy /usr/lib/tmpfiles.d/tmp.conf to /etc/tmpfiles.d/.

    [root@server1 ~]# cp /usr/lib/tmpfiles.d/tmp.conf /etc/tmpfiles.d/
  3. Find the line in /etc/tmpfiles.d/tmp.conf that controls the purging interval for /tmp, and change the interval from 10d to 5d. You can open /etc/tmpfiles.d/tmp.conf in an editor and make the change, or use the following command:

    [root@server1 ~]# sed -i '/^d .tmp /s/10d/5d/' /etc/tmpfiles.d/tmp.conf
  4. Test if systemd-tmpfiles --clean accepts the new configuration.

    [root@server1 ~]# systemd-tmpfiles --clean tmp.conf
  5. Create a new configuration file /etc/tmpfiles.d/gallifrey.conf with the following content:

    # Set up /run/gallifrey, owned by root with 0700 permissions
    # Files not used for 30 seconds will be automatically deleted
    d /run/gallifrey 0700 root root 30s
  6. Test your new configuration for creating /run/gallifrey.

    [root@server1 ~]# systemd-tmpfiles --create gallifrey.conf
    [root@server1 ~]# ls -ld /run/gallifrey
    drwx------. 2 root root Feb 19  10:29 /run/gallifrey
  7. Test the purging of your /run/gallifrey directory.

    1. Create a new file under /run/gallifrey.

      [root@server1 ~]# touch /run/gallifrey/companion
    2. Wait for at least 30 seconds.

      [root@server1 ~]# sleep 30s
    3. Have systemd-tmpfiles clean the /run/gallifrey directory.

      [root@server1 ~]# systemd-tmpfiles --clean gallifrey.conf
    4. Inspect the contents of /run/gallifrey.

      [root@server1 ~]# ls -l /run/gallifrey